package com.md24housecall.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.md24housecall.activity.R;
import com.md24housecall.manager.DimenManager;
import com.md24housecall.model.Partner;
import com.md24housecall.view.RatioImageView;

public class PartnerAdapter extends BaseAdapter {
	private List<Partner> listPartners;
	private Context mContext;
	
	public PartnerAdapter(Context context,List<Partner> listPartners){
		this.listPartners=listPartners;
		mContext=context;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listPartners.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View rowView = convertView;
		ViewHolder viewHolder;
		if (rowView == null) {
			LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			rowView = inflater.inflate(R.layout.partner_item, null);
			viewHolder = new ViewHolder();
			viewHolder.imvPartner = (RatioImageView) rowView.findViewById(R.id.imvPartner);
			viewHolder.imvPartner.setRatio(DimenManager.WIDTH_PARTNER_IMG, DimenManager.HEIGHT_PARTNER_IMG);
//			viewHolder.txvPartner = (TextView) rowView.findViewById(R.id.txvPartner);
			rowView.setTag(viewHolder);
		}
		viewHolder = (ViewHolder) rowView.getTag();
		AQuery aq = new AQuery(convertView);
		try{
			if(listPartners.get(position).getImageUrl().startsWith("http") || listPartners.get(position).getImageUrl().startsWith("https"))
				aq.id(viewHolder.imvPartner).image(listPartners.get(position).getImageUrl(),false, true, 0,  R.drawable.comingsoon_demo,BitmapFactory.decodeResource(rowView.getResources(), R.drawable.comingsoon_demo),AQuery.FADE_IN);
			else
				viewHolder.imvPartner.setImageResource(Integer.parseInt(listPartners.get(position).getImageUrl()));
		}catch (Exception e) {
			viewHolder.imvPartner.setImageResource(R.drawable.comingsoon_demo);
		}
		return rowView;
	}
	
	public static class ViewHolder {
		public TextView txvPartner;
		public RatioImageView imvPartner;
	}

}
